package cn.org.cute.ebocy.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import sun.misc.BASE64Encoder;

public class NormalUtil {

	public static int parseInt(String para) {
		int p = 0;
		if (para != null) {
			try {
				p = Integer.parseInt(para.trim().replaceAll(",", ""));
			} catch (NumberFormatException e) {
			}
		}
		return p;
	}
	
	public static long parseLong(String para) {
		long p = 0L;
		if (para != null) {
			try {
				p = Long.parseLong(para.trim().replaceAll(",", ""));
			} catch (NumberFormatException e) {
			}
		}
		return p;
	}

	public static boolean parseBool(String para) {
		boolean p = false;
		if (para != null) {
			try {
				p = Boolean.parseBoolean(para);
			} catch (NumberFormatException e) {
			}
		}
		return p;
	}

	public static double parseDouble(String para) {
		double p = 0;
		if (para != null) {
			try {
				p = Double.parseDouble(para.trim().replaceAll(",", ""));
			} catch (NumberFormatException e) {
			}
		}
		return p;
	}

	public static String implode(ArrayList<String[]> strs, String sepa,
			String lineSepa) {

		String[] tempStrs = new String[strs.size()];
		int i = 0;

		for (String[] temp : strs) {
			tempStrs[i++] = implode(temp, sepa);
		}

		return implode(tempStrs, lineSepa);

	}

	public static String implode(String[] strs) {

		return implode(strs, ",", false);

	}

	public static String implode(String[] strs, String sepa) {

		return implode(strs, sepa, false);

	}

	public static String implode(String[] strs, boolean withBlank) {

		return implode(strs, ",", withBlank);

	}

	/**
	 * 将字符串数组合成为一个字符串
	 * 
	 * @param strs
	 *            字符串数组
	 * @param sepa
	 *            字符串间的分隔符号
	 * @return 合成后的单个字符串
	 */
	public static String implode(String[] strs, String sepa, boolean withBlank) {

		if (isBlank(strs)) {
			return "";
		}

		StringBuffer sb = new StringBuffer();

		boolean have = false;

		for (String str : strs) {
			if (withBlank || !isBlank(str)) {

				if (have) {
					sb.append(sepa);
				}
				have = true;
				sb.append(str);
			}
		}
		return sb.toString();
	}

	/**
	 * 将字符串分解为多个字符串的数组
	 * 
	 * @param str
	 *            字符串
	 * @param sepa
	 *            字符串间的分隔符号
	 * @return 分解后的字符串数组
	 */
	public static String[] explode(String str, String sepa) {
		if (str == null) {
			return new String[] {};
		}
		return str.split(sepa);
	}

	/**
	 * 将字符串分解为多个字符串的数组
	 * 
	 * @param str
	 *            字符串
	 * @return 分解后的字符串数组
	 */
	public static String[] explode(String str) {
		return explode(str, ";");
	}

	public static String implode(Object[] objs) {

		if (isBlank(objs)) {
			return "";
		}

		String[] strs = new String[objs.length];
		for (int i = 0; i < objs.length; i++) {
			strs[i] = objs[i] == null ? "" : objs[i].toString();
		}

		return implode(strs);

	}

	/**
	 * 控制字符串长度
	 * 
	 * @param str
	 *            字符串
	 * @param 允许的最长长度
	 * @return 处理后的字符串
	 */

	public static String lengthCheck(String str, int len) {

		if (str == null) {
			return "";
		}

		if (len < str.length()) {
			return str.substring(0, len);
		}

		return str;

	}

	/**
	 * 检测某个值是否在数组中
	 * 
	 * @param values
	 *            值数组
	 * @param v
	 *            要检测的值
	 * @return 如果存在，返回在数组中的位置；如果不存在，返回-1
	 */
	/*
	 * public static int inRange(Object[] values,Object v){
	 * 
	 * if(values == null || values.length <= 0 || v == null){ return -1; }
	 * 
	 * for(int i=0;i<values.length;i++){ if(v.equals(values[i])){ return i; } }
	 * return -1; } public static int inRange(String[] values,String v){
	 * 
	 * return inRange(values,v,false);
	 * 
	 * }
	 * 
	 * public static boolean inRange(String[] values,String v,boolean
	 * caseSense){
	 * 
	 * if(values == null || values.length <= 0 || v == null){ return -1; }
	 * 
	 * if(!caseSense){ v = v.toUpperCase(); } String temp = null;
	 * 
	 * for(int i=0;i<values.length;i++){ temp = values[i]; if(!caseSense){ temp
	 * = temp.toUpperCase(); } if(v.equals(temp)){ return i; } } return -1; }
	 */

	/**
	 * 检测值是否为空；如果值为LIST，则检测其中的对象数量是否为0
	 * 
	 * @param o
	 *            要检测的值
	 * @return true or false
	 */

	public static boolean isBlank(Object o) {

		if (o == null) {
			return true;
		}

		if (Collection.class.isInstance(o)) {
			return ((Collection<?>) o).size() <= 0;
		}

		return o.toString().trim().length() <= 0;

	}

	/**
	 * 检测数组是否为空；其中只要有一个值不为空，则表示数组不为空
	 * 
	 * @param os
	 *            要检测的值
	 * @return true or false
	 */
	public static boolean isBlank(Object[] os) {

		for (int i = 0; os != null && i < os.length; i++) {

			if (!isBlank(os[i])) {
				return false;
			}

		}

		return true;
	}

	public static boolean isBlank(int[] os) {

		return os == null || os.length == 0;

	}

	public static boolean isBlank(byte[] os) {

		return os == null || os.length == 0;

	}

	/**
	 * 去除字符串首尾空格
	 * 
	 * @param o
	 *            要处理的值
	 * @return 去除空格后的值
	 */

	public static String trim(Object o) {

		if (o == null) {
			return "";
		}

		return o.toString().trim();

	}

	public static int[] merge(int[] values) {

		if (isBlank(values)) {
			return values;
		}

		int[] temp = new int[values.length];
		int idx = 0;

		for (int i = 0; i < values.length; i++) {
			if (!inRange(temp, 0, idx - 1, new int[] { values[i] })) {
				temp[idx] = values[i];
				idx++;
			}
		}

		int[] newTemp = new int[idx];
		System.arraycopy(temp, 0, newTemp, 0, idx);

		return newTemp;

	}

	public static String[] merge(String[] values) {

		if (isBlank(values)) {
			return values;
		}

		String[] temp = new String[values.length];
		int idx = 0;

		for (int i = 0; i < values.length; i++) {

			if (!inRange(temp, 0, idx - 1, values[i], false)) {
				temp[idx] = values[i];
				idx++;
			}
		}

		String[] newTemp = new String[idx];
		System.arraycopy(temp, 0, newTemp, 0, idx);

		return newTemp;

	}

	public static int[] except(int[] range, int[] except) {

		if (NormalUtil.isBlank(range) || NormalUtil.isBlank(except)) {
			return range;
		}

		int[] temp = new int[range.length];
		int idx = 0;
		for (int i = 0; i < range.length; i++) {
			if (!NormalUtil.inRange(except, new int[] { range[i] })) {
				temp[idx] = range[i];
				idx++;
			}
		}
		int[] newTemp = new int[idx];
		System.arraycopy(temp, 0, newTemp, 0, idx);
		return newTemp;

	}

	/**
	 * 去除字符串首尾空格，并限制字符串长度
	 * 
	 * @param o
	 *            要处理的值
	 * @param length
	 *            允许的字符串最长长度
	 * @return 去除空格后的值
	 */

	public static String trim(Object o, int length) {

		String str = trim(o);
		if (str.length() > length) {
			str = trim(str.substring(0, length));
		}

		return str;

	}

	/**
	 * 读取文本文件
	 * 
	 * @param f
	 *            要读取的文件
	 * @return 文本文件的内容；多行间以\r\n为隔
	 * @throws IOException
	 */

	public static String readTextFile(File f) throws IOException {

		return readTextFile(f, null);
	}

	public static String readTextFile(InputStream is) throws IOException {

		return readTextFile(is, null);

	}

	public static ArrayList<String> readTextFileInLine(File f)
			throws IOException {

		return readTextFileInLine(f, null);
	}

	public static String readTextFile(File f, String encode) throws IOException {

		BufferedReader br = NormalUtil.isBlank(encode) ?

		new BufferedReader(new InputStreamReader(new FileInputStream(f)))
				: new BufferedReader(new InputStreamReader(new FileInputStream(
						f), encode));

		String line;
		StringBuffer sb = new StringBuffer();
		while ((line = br.readLine()) != null) {
			sb.append(line);
			sb.append("\r\n");
		}
		return sb.toString();
	}

	public static String readTextFile(InputStream is, String encode)
			throws IOException {

		StringBuffer sb = new StringBuffer();
		String line = null;

		BufferedReader br = NormalUtil.isBlank(encode) ?

		new BufferedReader(new InputStreamReader(is)) : new BufferedReader(
				new InputStreamReader(is, encode));

		while ((line = br.readLine()) != null) {
			sb.append(line);
			sb.append("\r\n");
		}

		return sb.toString();

	}

	public static ArrayList<String> readTextFileInLine(File f, String encode)
			throws IOException {

		BufferedReader br = NormalUtil.isBlank(encode) ?

		new BufferedReader(new InputStreamReader(new FileInputStream(f)))
				: new BufferedReader(new InputStreamReader(new FileInputStream(
						f), encode));
		String line;
		ArrayList<String> al = new ArrayList<String>();
		while ((line = br.readLine()) != null) {
			al.add(line);
		}
		return al;
	}

	public static boolean writeBinaryFile(File folder, String fileName,
			byte[] content) throws IOException {

		return writeBinaryFile(new File(folder, fileName), content);

	}

	public static boolean writeBinaryFile(String folder, String fileName,
			byte[] content) throws IOException {

		return writeBinaryFile(new File(folder, fileName), content);

	}

	public static boolean writeBinaryFile(File f, byte[] content)
			throws IOException {

		File folder = f.getParentFile();

		if (!folder.exists()) {
			folder.mkdirs();
		}

		BufferedOutputStream bos = new BufferedOutputStream(
				new FileOutputStream(f));
		bos.write(content == null ? new byte[0] : content);
		bos.close();

		return true;

	}

	public static boolean deleteFile(File f) throws IOException {

		if (f != null || f.exists()) {
			return f.delete();
		}

		return true;
	}

	public static byte[] readBinaryFile(File f) throws IOException {

		ByteArrayOutputStream baos = new ByteArrayOutputStream();

		if (f == null || !f.exists()) {
			return baos.toByteArray();
		}

		BufferedInputStream bis = new BufferedInputStream(
				new FileInputStream(f));

		byte[] temp = new byte[256];
		int len = 0;

		while ((len = bis.read(temp)) > 0) {
			baos.write(temp, 0, len);
		}

		bis.close();

		return baos.toByteArray();

	}

	public static boolean inRange(int[] range, int value) {
		return inRange(range, new int[] { value });
	}

	public static boolean inRange(int[] range, int[] value) {
		return inRange(range, 0, range.length - 1, value);
	}

	public static boolean inRange(int[] range, int rangeBegin, int rangeEnd,
			int value) {
		return inRange(range, rangeBegin, rangeEnd, new int[] { value });
	}

	public static boolean inRange(int[] range, int rangeBegin, int rangeEnd,
			int[] value) {

		if (isBlank(range) && !isBlank(value)) {
			return false;
		}

		if (isBlank(value)) {
			return true;
		}

		int vl = value.length;
		boolean find = false;

		for (int i = 0; i < vl; i++) {
			find = false;
			for (int m = rangeBegin; m <= rangeEnd; m++) {
				if (value[i] == range[m]) {
					find = true;
					break;
				}
			}
			if (!find) {
				return false;
			}
		}
		return true;
	}

	public static boolean inRange(String[] range, String value) {
		return inRange(range, new String[] { value }, false);
	}

	public static boolean inRange(String[] range, String value,
			boolean caseSense) {
		return inRange(range, new String[] { value }, caseSense);
	}

	public static boolean inRange(String[] range, String[] value) {
		return inRange(range, value, false);
	}

	public static boolean inRange(String[] range, String[] value,
			boolean caseSense) {
		return inRange(range, 0, range.length - 1, value, caseSense);
	}

	public static boolean inRange(String[] range, int rangeBegin, int rangeEnd,
			String value, boolean caseSense) {
		return inRange(range, rangeBegin, rangeEnd, new String[] { value },
				caseSense);
	}

	public static <K, T> T findInRange(ArrayList<T> range, T value,
			Searchable<K, T> searcher) {

		if (isBlank(range) || value == null) {
			return null;
		}

		K key = searcher.getKey(value);

		for (T r : range) {
			if (r == null) {
				continue;
			}
			if (searcher.getKey(r).equals(key)) {
				return r;
			}
		}

		return null;

	}

	public static boolean inRange(String[] range, int rangeBegin, int rangeEnd,
			String[] value, boolean caseSense) {

		if (isBlank(range) && !isBlank(value)) {
			return false;
		}

		if (isBlank(value)) {
			return true;
		}

		String[] rangeTemp = new String[range.length];
		for (int i = 0; i < range.length; i++) {
			rangeTemp[i] = caseSense ? trim(range[i]) : trim(range[i])
					.toUpperCase();
		}

		String[] valueTemp = new String[value.length];
		for (int i = 0; i < value.length; i++) {
			valueTemp[i] = caseSense ? trim(value[i]) : trim(value[i])
					.toUpperCase();
		}

		return inRangeAfterCheck(rangeTemp, rangeBegin, rangeEnd, valueTemp);

	}

	private static boolean inRangeAfterCheck(String[] range, int rangeBegin,
			int rangeEnd, String[] value) {

		int vl = value.length;
		boolean find = false;
		String a = null;
		String b = null;

		for (int i = 0; i < vl; i++) {
			find = false;
			a = value[i];
			for (int m = rangeBegin; m <= rangeEnd; m++) {
				b = range[m];
				// System.out.println(a+":"+b);
				if (a.equals(b)) {
					find = true;
					break;
				}
			}
			if (!find) {
				return false;
			}
		}
		return true;
	}

	public static <K, T> ArrayList<K> getKeys(ArrayList<T> al,
			Searchable<K, T> searcher) {

		if (isBlank(al)) {
			return null;
		}

		ArrayList<K> keys = new ArrayList<K>();

		for (T t : al) {
			keys.add(searcher.getKey(t));
		}

		return keys;

	}

	public static <K, T> HashMap<K, T> listToHash(List<T> list,
			Searchable<K, T> searcher) {

		if (list == null) {
			return null;
		}

		HashMap<K, T> ht = new HashMap<K, T>();

		for (T temp : list) {
			ht.put(searcher.getKey(temp), temp);
		}

		return ht;

	}

	public static <K, T> HashMap<K, ArrayList<T>> listToHashList(List<T> list,
			Searchable<K, T> searcher) {

		HashMap<K, ArrayList<T>> ht = new HashMap<K, ArrayList<T>>();

		if (isBlank(list)) {
			return ht;
		}

		ArrayList<T> tempList = null;
		K key = null;

		for (T temp : list) {
			key = searcher.getKey(temp);
			tempList = ht.get(key);
			if (tempList == null) {
				tempList = new ArrayList<T>();
				ht.put(key, tempList);
			}
			tempList.add(temp);
		}

		return ht;

	}

	public static String subStrCaseIns(String str, String begin, String end) {

		if (NormalUtil.isBlank(str) || NormalUtil.isBlank(begin)
				|| NormalUtil.isBlank(end)) {
			return null;
		}

		String strCase = str.toUpperCase();
		String beginCase = begin.toUpperCase();
		String endCase = end.toUpperCase();

		int posBegin = 0;
		int posBeginAfter = 0;
		int posEnd = str.length();

		posBegin = strCase.indexOf(beginCase);
		if (posBegin < 0) {
			return null;
		}
		posBeginAfter = posBegin + begin.length();

		posEnd = strCase.indexOf(endCase, posBeginAfter);
		if (posEnd < 0) {
			return null;
		}

		return str.substring(posBeginAfter, posEnd);

	}

	public static String subStr(String str, String begin, String end) {

		if (NormalUtil.isBlank(str) || NormalUtil.isBlank(begin)
				|| NormalUtil.isBlank(end)) {
			return null;
		}

		int posBegin = 0;
		int posBeginAfter = 0;
		int posEnd = str.length();

		posBegin = str.indexOf(begin);
		if (posBegin < 0) {
			return null;
		}
		posBeginAfter = posBegin + begin.length();

		posEnd = str.indexOf(end, posBeginAfter);
		if (posEnd < 0) {
			return null;
		}

		return str.substring(posBeginAfter, posEnd);

	}

	public static String[] getInputParameters(String msg) throws IOException {

		//System.out.print(msg);

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		String[] temp = br.readLine().trim().split(" ");

		return temp;

	}

	public static double[][] colRowSwitch(double[][] values) {

		int rows = values.length;
		int cols = values[0].length;

		double[][] temp = new double[cols][rows];

		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < cols; j++) {
				temp[j][i] = values[i][j];
			}
		}

		return temp;

	}

	public static Object[][] colRowSwitch(Object[][] values) {

		if (values == null || values.length < 1) {
			return null;
		}

		int rows = values.length;
		int cols = values[0].length;

		Object[][] temp = new Object[cols][rows];

		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < cols; j++) {
				temp[j][i] = values[i][j];
			}
		}

		return temp;

	}

	public static double[] parseDouble(String[] str) {

		if (str == null) {
			return null;
		}

		return parseDouble(str, 0, str.length - 1);

	}

	public static double[] parseDouble(String[] str, int beginPos, int endPos) {

		if (str == null) {
			return null;
		}

		double[] values = new double[endPos - beginPos + 1];

		int n = 0;

		for (int i = beginPos; i <= endPos; i++) {
			values[n++] = Double.parseDouble(str[i]);
		}

		return values;

	}

	public static double[] addInFixArray(double[] values, double v,
			boolean atEnd) {

		if (!atEnd) {
			for (int i = values.length - 1; i > 0; i--) {
				values[i] = values[i - 1];
			}

			values[0] = v;
		} else {
			for (int i = 0; i < values.length - 1; i++) {
				values[i] = values[i + 1];
			}

			values[values.length - 1] = v;
		}
		return values;

	}

	/**
	 * 判断字符串是否为空
	 * 
	 * @param param
	 *            校验的字符串
	 * @return true|false
	 */
	public static boolean isNull(String param) {
		if (param == null || "".equals(param) || "null".equalsIgnoreCase(param)
				|| "undefined".equalsIgnoreCase(param)) {
			return true;
		}
		return false;
	}

	/**
	 * 判断是否是否全是数字
	 * 
	 * @param param
	 *            校验的字符串
	 * @return true|false
	 */
	public static boolean isNumber(String param) {
		if (param != null && !param.equals("")) {
			String reg = "\\d+";
			Pattern p = Pattern.compile(reg);
			Matcher m = p.matcher(param);
			return m.matches();
		}
		return false;
	}

	/**
	 * ArrayList对象转化为数组
	 * 
	 * @param opList
	 * @return
	 */
	public static String[] toArray(List values) {
		if (values == null || values.size() == 0)
			return null;
		String[] valueTo = new String[values.size()];
		values.toArray(valueTo);
		return valueTo;
	}

	/**
	 * 获得connectInfo
	 * 
	 * @param paramsUrl
	 * @return
	 */
	public static String getURLConnectionInfo(String paramsUrl) {
		if (isNull(paramsUrl)) {
			return null;
		}
		if (!paramsUrl.startsWith("http")) {
			paramsUrl += "http://";
		}

		StringBuffer document = new StringBuffer();
		try {
			URL url = new URL(paramsUrl);
			URLConnection conn = url.openConnection();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					conn.getInputStream(), "UTF-8"));
			String line = null;
			while ((line = reader.readLine()) != null)
				document.append(line + "\n");
			reader.close();
		} catch (Exception e) {
			e.printStackTrace();
			System.err.println("Unable to connect to URL: " + paramsUrl);
			return null;
		}
		return document.toString().trim();
	}
	
	  /**
	   * 根据时间获得订单ID
	   * @return
	   */
	 public static String getOrderId(){
		  Date date = new Date();
		  SimpleDateFormat fomatf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		  String orderId = fomatf.format(date);
		 return orderId;
		 	  
	 }
	 
	  //BASE64Encoder 编码
	  public static String TOBASE64Encoder(String encoderCode ){
		  if(isNull(encoderCode)){
			 return null; 
		  }
		  try{
		   BASE64Encoder base64=new BASE64Encoder();
		   return  base64.encode(encoderCode.getBytes("utf-8"));
		  }catch(Exception e){
			  return null;
		  }
	  }

	  /**
		 * 替换url中的*,_号
		 * @param url
		 * @return
		 */
		public static String replaceUrl(String url,boolean isUrl){
			url = url.replaceAll("[-]", "%2D");
			url = url.replaceAll("[,]", "%2C");
			url = url.replaceAll("[+]", "%2B");
			if(!isUrl){
				url = url.replaceAll("[*]", "%2A");
			}
			return url;
		}
	
	  
	public static void main(String[] s) {
		String playInfoIds = "2736,2735,2734,2733,2731,2730,2729,2728,2727,2726,2725,2724,2723,2722,2721,2720,2719,2718,2716,2715,2714,2713";
		String[] uids = playInfoIds.split(",");
		playInfoIds = playInfoIds.replaceFirst(uids[0]+",", "");
		playInfoIds  += ","+"111111111";
   System.out.println(playInfoIds);

	}
	


}
